<template>
  <div>
    <el-row :gutter="4" type="flex" justify="start">
      <el-col :span="24">
        <span>关键词:</span>
        <el-input v-model="username" placeholder="用户名" style="width:200px;" @keyup.enter.native="handleFilter" />
        <el-button type="primary" @click="handleFilter">搜索</el-button>
        <el-button size="medium" type="info" @click="clearParams">清空</el-button>
      </el-col>
    </el-row>
    <ul class="coincheck">
      <li>
        网站总发放 :
        <span>{{ sum }}</span> 个
      </li>
      <li>
        网站总回收 :
        <span>{{ recycle }}</span> 个
      </li>
      <li>
        网站待回收 :
        <span>{{ precycle }}</span> 个
      </li>
    </ul>
    <subtable
      :table-label="tableLabel"
      :table-data="tableData.slice((currentpage-1)*pagesize,currentpage*pagesize)"
    >
      <el-table-column slot="first" :min-width="15" label="用户" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="handleClickDialog(scope)">{{ scope.row.userName }}</el-button>
        </template>
      </el-table-column>
      <el-table-column slot="button" :min-width="15" label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="primary" @click="getDetail(scope)">查看</el-button>
        </template>
      </el-table-column>
    </subtable>
    <pagination
      :total="total"
      :current-page="currentpage"
      :page-size="pagesize"
      @handleSizeChange="handleSizeChange"
      @handleCurrentChange="handleCurrentChange"
    />
    <coin-dialog
      :visiabledialog="coinDialog"
      :dialog-data="coinData"
      @changeVisiable="coinDialog = false"
    />
    <detail-dialog
      :visiabledialog="detailDialog"
      :dialog-data="detailData"
      @change="detailDialog = false"
    />
  </div>
</template>

<script>
import CoinDialog from '@/components/Dialog'
import DetailDialog from './dialog'
import Subtable from '@/components/Eltable'
import Pagination from '@/components/Pagination' // 分页组件
import { getCoinManageList, getCoinConsumeDetailList } from '@/api/deal/virtualcoin' // 数据接口
import { getUserDetail } from '@/api/deal/userDetail' // 数据接口
import { getTenantId } from '@/utils/auth'
export default {
  components: {
    Subtable,
    Pagination,
    CoinDialog,
    DetailDialog
  },
  data() {
    return {
      detailData: {},
      detailDialog: false,
      coinDialog: false,
      coinData: {},
      total: 0,
      currentpage: 1,
      pagesize: 10,
      sum: 0,
      recycle: 0,
      precycle: 0,
      username: '',
      tableLabel: [
        {
          label: '充值', width: '10', prop: 'rechargeAmount'
        }, {
          label: '消费', width: '10', prop: 'purchaseAmount'
        }, {
          label: '账户余额', width: '10', prop: 'amount'
        }
      ],
      tableData: [],
      listFilter: []
    }
  },
  created() {
    this.getData()
  },
  updated() {
    this.statusfil()
  },
  methods: {
    handleClickDialog(scope) {
      this.coinDialog = true
      getUserDetail(scope.row.userId).then(response => {
        this.coinData = response.data.item
      })
    },
    handleSizeChange(val) {
      console.log(val)
      this.currentpage = 1
      this.pagesize = val.value
      if (this.pagesize >= 20) {
        scrollTo(50, 800)
      }
    },
    handleCurrentChange(val) {
      console.log(val)
      this.currentpage = val.value
      if (this.pagesize >= 20) {
        scrollTo(50, 800)
      }
    },
    statusfil() {
      let sum = 0
      let recycle = 0
      let precycle = 0
      this.tableData.map(item => {
        sum += item.rechargeAmount
        recycle += item.purchaseAmount
        precycle += item.amount
      })
      this.sum = sum
      this.recycle = recycle
      this.precycle = precycle
    },
    handleFilter() {
      this.tableData = this.listFilter.filter(item => {
        if (this.username && item.userName.toLowerCase().indexOf(this.username.toLocaleLowerCase()) === -1) return false
        return true
      })
      this.total = this.tableData.length
    },
    clearParams() {
      this.username = ''
      this.getData()
    },
    getData() {
      this.listLoading = true
      getCoinManageList().then(response => {
        this.tableData = response.data.items.filter(item => {
          if (item.tenantId !== getTenantId()) return false
          return true
        })
        this.listFilter = this.tableData
        this.total = this.tableData.length
        this.listLoading = false
      })
    },
    getDetail(scope) {
      this.detailDialog = true
      const data = scope.row
      getCoinConsumeDetailList(scope.row.userId).then(response => {
        data.tableData = response.data.items
        this.detailData = data
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.coincheck {
  list-style: none;
  padding: 0;
  padding-left: 11px;
  margin: 0;
  margin-bottom: 21px;
  margin-top: 14px;
  height: 44px;
  line-height: 44px;
  box-sizing: border-box;
  background-color: #f8f8f8;
  li {
    float: left;
    display: block;
    margin-right: 20px;
    color: #72726f;
    span {
      color: rgb(15, 202, 62);
      font-weight: 600;
    }
  }
}
</style>
